﻿/*=========================================================================
 * AUTH: Vincent Breton
 * DESC: Game State Manager
 *=========================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace RisingThreat
{
    class StateManager
    {
        Game1 game;
        ContentManager content;
        GraphicsDeviceManager graphics;
        GameScreen gameScreen;
        TitleScreen titleScreen;
        NumberOfPlayersScreen numberOfPlayersScreen;
        PauseScreen pauseScreen;
        GameOverScreen gameOverScreen;
        CreditScreen creditScreen;
        HelpScreen helpScreen;
        Screen currentScreen;

        public StateManager(ContentManager cont, GraphicsDeviceManager graphs, Game1 g)
        {
            content = cont;
            graphics = graphs;
            game = g;
            titleScreen = new TitleScreen(content, new EventHandler(TitleScreenEvent), graphics);
            numberOfPlayersScreen = new NumberOfPlayersScreen(content, new EventHandler(NumberOfPlayerScreenEvent), graphics);
            pauseScreen = new PauseScreen(content, new EventHandler(PauseScreenEvent), graphics);
            gameOverScreen = new GameOverScreen(content, new EventHandler(GameOverScreenEvent), graphics);

            currentScreen = titleScreen;
        }

        public void GameScreenEvent(object obj, EventArgs e)
        {
            ScreenArgs ev = (ScreenArgs)e;
            if (ev.arg == 0)
            {
                currentScreen = pauseScreen;
            }
            else if (ev.arg == 1)
            {
                currentScreen = gameOverScreen;
            }

        }

        public void CreditScreenEvent(object obj, EventArgs e)
        {

            titleScreen = new TitleScreen(content, new EventHandler(TitleScreenEvent), graphics);
            currentScreen = titleScreen;
        }


        public void GameOverScreenEvent(object obj, EventArgs e)
        {
            titleScreen = new TitleScreen(content, new EventHandler(TitleScreenEvent), graphics);
            currentScreen = titleScreen;
        }

        public void TitleScreenEvent(object obj, EventArgs e)
        {
            ScreenArgs ev = (ScreenArgs)e;
            if (ev.arg == 0)
            {
                numberOfPlayersScreen.setFirstTimeInMenu(true);
                currentScreen = numberOfPlayersScreen;
            }
            else if (ev.arg == 1)
            {
                helpScreen = new HelpScreen(content, new EventHandler(CreditScreenEvent), graphics);
                currentScreen = helpScreen;
            }
            else if (ev.arg == 2)
            {
                creditScreen = new CreditScreen(content, new EventHandler(CreditScreenEvent), graphics);
                currentScreen = creditScreen;
            }
            else if (ev.arg == 3)
            {
                game.Exit();
            }
        }


        public void NumberOfPlayerScreenEvent(object obj, EventArgs e)
        {
            ScreenArgs ev = (ScreenArgs)e;
            if (ev.arg == 0)
            {
                gameScreen = new GameScreen(content, new EventHandler(GameScreenEvent), graphics);
                currentScreen = gameScreen;
            }
            else if (ev.arg == 1)
            {
                titleScreen.setFirstTimeInMenu(true);
                currentScreen = titleScreen;
            }
        }

        public void PauseScreenEvent(object obj, EventArgs e)
        {
            ScreenArgs ev = (ScreenArgs)e;
            if (ev.arg == 0)
            {
                currentScreen = gameScreen;
                gameScreen.playMusic();
            }
            else if (ev.arg == 1)
            {
                titleScreen = new TitleScreen(content, new EventHandler(TitleScreenEvent), graphics);
                currentScreen = titleScreen;
            }
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            currentScreen.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Draw(SpriteBatch batch)
        {
            currentScreen.Draw(batch);
        }

    }
}
